/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */



 let tree = {
    val: 1,
    left: {
        val: 2,
        left: {
            val: 4,
            left: null,
            right: null
        },
        right: {
            val: 5,
            left: null,
            right: null
        }
    },
    right: {
        val: 3,
        left: {
            val: 6,
            left: null,
            right: null
        },
        right: {
            val: 7,
            left: null,
            right: null
        }
    }
}



//递归方法实现
var inorderTraversal = function (root) {
    var traversal = (node, arr) => {
        if (node.left != null) {
            traversal(node.left, arr)
        }
        arr.push(node.val)
        if (node.right != null) {
            traversal(node.right, arr)
        }
        return
    }
    let res = []
    if (root) {
        traversal(root, res)
    }
    return res;
};


//非递归实现
var _inorderTraversal = function (root) {
    let stack = []
    let ans = []
    if(root){
        while (stack.length > 0 || root!=null) {
            debugger
            while (root != null) {
                stack.push(root)
                root = root.left
            }
            if (stack.length > 0) {
                let tmp = stack.pop()
                ans.push(tmp.val)
                root = tmp.right
            }
        }
    }
    return ans
}

console.log(_inorderTraversal(tree))